package com.taropan.wallpaper.ui;

import java.util.List;

import com.google.gson.Gson;
import com.taropan.wallpaper.R;
import com.taropan.wallpaper.model.CategoryEntry;
import com.taropan.wallpaper.model.CategoryInfo;
import com.taropan.wallpaper.model.CategoryListAdapter;
import com.taropan.wallpaper.model.KeywordEntry;
import com.taropan.wallpaper.model.KeywordInfo;
import com.taropan.wallpaper.model.LastUpdatedWallpaper;
import com.taropan.wallpaper.model.PugListAdapter;
import com.taropan.wallpaper.model.ResolutionEntry;
import com.taropan.wallpaper.util.Consts;
import com.taropan.wallpaper.util.HttpUtils;
import com.taropan.wallpaper.util.UICore;
import com.taropan.wallpaper.util.Utils;
import com.taropan.wallpaper.widget.CustomGridView;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

public class CategoryActivity extends BaseActivity implements Callback {

	
	private static final String TAG = CategoryActivity.class.getSimpleName();
	private static final int EXEU_GET_CATEGORY_INFO = 0;
	private Handler handler;
	private GridView mGridView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_category);
		Utils.dout(TAG, "onCreate");
		initHead();
		tv_head.setVisibility(View.INVISIBLE);
		handler = new Handler(this);
		
		mGridView = (GridView) findViewById(R.id.gridView_category);
		
		UICore.eventTask(this, CategoryActivity.this, EXEU_GET_CATEGORY_INFO, "", null);
		
	}
	
	@Override
	public void execute(int mes, Object obj) {
		super.execute(mes, obj);
		switch (mes) {
		case EXEU_GET_CATEGORY_INFO:
			getCategorylist(); 
			break;

		default:
			break;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Utils.dout(TAG, "onResume");
	}
	
	private void getCategorylist() {
		StringBuffer url = new StringBuffer(Consts.API_WALLPAPER_CATEGORY);
		Utils.dout("getCategorylist url: " + url.toString());
		try {
			
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			Utils.dout("getCategorylist results:" + results.toString());
			Gson gson = new Gson();
			CategoryInfo categoryInfo = gson.fromJson(results, CategoryInfo.class);
			List<CategoryEntry> wallpaperCategorys = categoryInfo.getWallpaperSort();
			Message localMessage = Message.obtain(handler, EXEU_GET_CATEGORY_INFO, wallpaperCategorys);
			handler.sendMessage(localMessage);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case EXEU_GET_CATEGORY_INFO:
			List<CategoryEntry> wallpaperCategorys = (List<CategoryEntry>) msg.obj;
			if (!wallpaperCategorys.isEmpty()) {
				CategoryListAdapter adapter = new CategoryListAdapter(CategoryActivity.this, wallpaperCategorys);
				mGridView.setAdapter(adapter);
			}
			break;

		default:
			break;
		}
		return false;
	}
}
